package com.tm.controller;

import com.tm.pojo.SysuserPojo;
import com.tm.pojo.vo.SysuserVo;
import com.tm.service.SysuserService;
import com.tm.utils.FileTools;
import com.tm.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("login")
public class SysuserController {
    @Autowired
    private SysuserService sysuserService;

    @PostMapping("login")
    public Map<String ,String>login(@RequestBody SysuserPojo sysuser){
        Map<String, String> map = new HashMap<>();
        SysuserPojo sysuserPojo= sysuserService.login(sysuser.getUsername(),sysuser.getPassword());
        if(sysuserPojo!=null){
            map.put("code","200");
            map.put("data","登陆成功");
            map.put("user","sysuserPojo");
        }else {
            map.put("code","201");
            map.put("data","登陆失败，账号或密码错误");
        }
        return map;
    }
    //查询开始了噢
    @GetMapping("querySysuser")
    public PageUtil querySysuser(SysuserVo sysuserVo){
        return sysuserService.querySysuser(sysuserVo);
    }
    //新增开始了
    @PostMapping("addSysuser")
    public Map<String,String> addSysuser(@RequestBody SysuserPojo sysuserPojo){
        Map<String, String> map = new HashMap<>();
        sysuserService.addSysuser(sysuserPojo);
        map.put("code","200");
        map.put("data","新增成功");
        return map;
    }

    //文件上传
    @PostMapping("uploadFile")
    public Map<String,String>uploadFile(MultipartFile multipartFile, HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        String fileName = FileTools.uploadFile(multipartFile, request);
        map.put("code","200");
        map.put("data",fileName);
        return map;
    }
    //文件下载
    @GetMapping("downlodFile")
    public void downlodFile(String fileName, HttpServletRequest request, HttpServletResponse response){
        FileTools.downlodFile(fileName,request,response);
    }
    //修改开始了
    @PutMapping("updateSysuser")
    public Map<String,String> updateSysuser(@RequestBody SysuserPojo sysuserPojo){
        Map<String, String> map = new HashMap<>();
        sysuserService.updateSysuser(sysuserPojo);
        map.put("code","200");
        map.put("data","修改成功");
        return map;
    }
    //删除开始了
    @DeleteMapping("deleteSysuser")
    public Map<String,String> deleteSysuser(Integer id){
        Map<String, String> map = new HashMap<>();
        sysuserService.deleteSysuser(id);
        map.put("code","200");
        map.put("data","修改成功");
        return map;
    }
    //劈山开始了
    @DeleteMapping("deleteAll/{str}")
    public Map<String,String> deleteAll(@PathVariable String [] str){
        Map<String, String> map = new HashMap<>();
        sysuserService.deleteAll(str);
        map.put("code","200");
        map.put("data","删除成功");
        return map;
    }
}
